using System.Collections.Generic;
using System.Data;
public static class JsonMethods
{
    private static List<Dictionary<string, object>>
        RowsToDictionary(DataTable table)
    {
        List<Dictionary<string, object>> objs =
            new List<Dictionary<string, object>>();
        foreach (DataRow dr in table.Rows)
        {
            Dictionary<string, object> drow = new Dictionary<string, object>();
            for (int i = 0; i < table.Columns.Count; i++)
            {
                if (table.Columns[i].DataType.Name.ToLower() == "datetime")
                {
                    drow.Add(table.Columns[i].ColumnName, GetFormatedDateTime(dr[i].ToString()));
                }
                else if (table.Columns[i].DataType.Name.ToLower() == "boolean")
                {
                    drow.Add(table.Columns[i].ColumnName, dr[i].ToString().Trim() != "" ? System.Convert.ToBoolean(dr[i].ToString()) : false);
                }
                else
                {
                    drow.Add(table.Columns[i].ColumnName, dr[i]);
                }
            }
            objs.Add(drow);
        }
        if (table.Rows.Count == 0)
        {
            objs = null;
        }

        return objs;
    }

    public static Dictionary<string, object> ToJson(DataTable table)
    {
        Dictionary<string, object> d = new Dictionary<string, object>();
        if (table.TableName.ToLower().Trim() != "" && table.TableName.ToLower().Trim() != "table")
            d.Add(table.TableName, RowsToDictionary(table));
        else
            d.Add("data", RowsToDictionary(table));

        //d.Add(table.TableName.ToLower().Trim() != "table" ? table.TableName : "Table", RowsToDictionary(table));
        //Dictionary<string, object> result = new Dictionary<string, object>();
        //result.Add("Data", d);
        //return result;
        return d;
    }

    public static Dictionary<string, object> ToJson(DataSet data)
    {
        Dictionary<string, object> d = new Dictionary<string, object>();
        foreach (DataTable table in data.Tables)
        {
            if (table.TableName.ToLower().Trim() != "" && table.TableName.ToLower().Trim() != "table")
                d.Add(table.TableName, RowsToDictionary(table));
            else
                d.Add("data", RowsToDictionary(table));
            //d.Add(table.TableName.ToLower().Trim() != "table" ? table.TableName : "Table", RowsToDictionary(table));
        }
        //Dictionary<string, object> result = new Dictionary<string, object>();
        //result.Add("Data", d);
        //return result;
        return d;
    }
    private static string GetFormatedDateTime(string input)
    {
        string date = "";
        try
        {
            date = input;
            if (input.Trim() != "")
            {
                System.DateTime datetime = System.DateTime.Parse(input);
                //date = datetime.ToString("o");
                //date = datetime.ToString("yyyy-MM-ddTHH\:mm\:ss.fffffffzzz");
                date = datetime.ToString("s");
                //"yyyy-MM-ddTHH\:mm\:ss.fffffffzzz"
            }
        }
        catch
        {
        }
        return date;
    }
}